package com.honey.mybatis.generator.criteria;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.ClassDecoration;
import com.honey.compilation.java.Decoration;
import com.honey.compilation.java.EnumField;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.compiler.CompilationException;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractClass;

public class TermClass extends AbstractClass{
	
	private static final String[] JAVA_DOC = new String[]{
			"判断条件"
	};
	
	private static final FullyQualifiedJavaType type = new FullyQualifiedJavaType(CriteriaConstant.TERM_CLASS_NAME);
	
	private static final  FullyQualifiedJavaType ENUM_FIELD_TYPE = JDKFullyQualifiedJavaType.getStringInstance();
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractClass#getAbstractJavaCompilation()
	 */
	@Override
	protected AbstractJavaCompilation getAbstractJavaCompilation() throws CompilationException {
		InnerEnum inner =new InnerEnum(type);
		isNullEnumField(inner);
		isNotNullEnumField(inner);
		isEqualToEnumField(inner);
		isNotEqualToEnumField(inner);
		isGreaterThanEnumField(inner);
		isGreaterThanOrEqualToEnumField(inner);
		isLessThanEnumField(inner);
		isLessThanOrEqualToEnumField(inner);
		isInEnumField(inner);
		isNotInEnumField(inner);
		isBetweenEnumField(inner);
		isNotBetweenEnumField(inner);
		isLikeEnumField(inner);
		isNotLikeEnumField(inner);
		addField(inner);
		addMethod(inner);
		return inner;
	}
	
	private void addMethod(InnerEnum inner) {
		Method method = new Method(inner.getType().getShortName(), true);
		Decoration d = method.getDecoration();
		d.setVisibility(JavaVisibility.PRIVATE);
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"value");
		method.addParameter(parameter);
		method.addBodyLine("this.value = value");
		inner.addMethod(method);
		
		method = new Method("toString");
		method.setReturnType(ENUM_FIELD_TYPE);
		method.addBodyLine("return this.value");
		method.addAnnotation("Override");
		inner.addMethod(method);
	}
	
	private void isNullEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,CriteriaConstant.IS_NULL_FIELD_NAME);
		EnumField enumField = new EnumField(CriteriaConstant.IS_NULL_FIELD_NAME,"is null 条件") ;
		enumField.addParameter(parameter, "IS NULL");
		inner.addEnumFields(enumField);
	}
	
	private void isNotNullEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,CriteriaConstant.IS_NOT_NULL_FIELD_NAME);
		EnumField enumField = new EnumField(CriteriaConstant.IS_NOT_NULL_FIELD_NAME,"is not null 条件") ;
		enumField.addParameter(parameter, "IS NOT NULL");
		inner.addEnumFields(enumField);
	}
	
	private void isEqualToEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,CriteriaConstant.EQUAL_TO_FIELD_NAME);
		EnumField enumField = new EnumField(CriteriaConstant.EQUAL_TO_FIELD_NAME,"= 条件") ;
		enumField.addParameter(parameter, "=");
		inner.addEnumFields(enumField);
	}
	
	private void isNotEqualToEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"NOT_EQUAL_TO");
		EnumField enumField = new EnumField("NOT_EQUAL_TO","<> 条件") ;
		enumField.addParameter(parameter, "<>");
		inner.addEnumFields(enumField);
	}
	
	private void isGreaterThanEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"GREATER_THAN");
		EnumField enumField = new EnumField("GREATER_THAN","> 条件") ;
		enumField.addParameter(parameter, ">");
		inner.addEnumFields(enumField);
	}
	
	private void isGreaterThanOrEqualToEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"GREATER_THAN_OR_EQUAL_TO");
		EnumField enumField = new EnumField("GREATER_THAN_OR_EQUAL_TO",">= 条件") ;
		enumField.addParameter(parameter, ">=");
		inner.addEnumFields(enumField);
	}
	
	private void isLessThanEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"LESS_THAN");
		EnumField enumField = new EnumField("LESS_THAN","< 条件") ;
		enumField.addParameter(parameter, "<");
		inner.addEnumFields(enumField);
	}
	
	private void isLessThanOrEqualToEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"LESS_THAN_OR_EQUAL_TO");
		EnumField enumField = new EnumField("LESS_THAN_OR_EQUAL_TO","<= 条件") ;
		enumField.addParameter(parameter, "<=");
		inner.addEnumFields(enumField);
	}
	
	private void isInEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"IN");
		EnumField enumField = new EnumField("IN","in 条件") ;
		enumField.addParameter(parameter, "IN");
		inner.addEnumFields(enumField);
	}

	private void isNotInEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"NOT_IN");
		EnumField enumField = new EnumField("NOT_IN","not in 条件") ;
		enumField.addParameter(parameter, "NOT IN");
		inner.addEnumFields(enumField);
	}

	private void isBetweenEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"BETWEEN");
		EnumField enumField = new EnumField("BETWEEN","between 条件") ;
		enumField.addParameter(parameter, "BETWEEN");
		inner.addEnumFields(enumField);
	}
	
	private void isNotBetweenEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"NOT_BETWEEN");
		EnumField enumField = new EnumField("NOT_BETWEEN","not between 条件") ;
		enumField.addParameter(parameter, "NOT BETWEEN");
		inner.addEnumFields(enumField);
	}
	
	private void isLikeEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"LIKE");
		EnumField enumField = new EnumField("LIKE","like 条件") ;
		enumField.addParameter(parameter, "LIKE");
		inner.addEnumFields(enumField);
	}
	
	private void isNotLikeEnumField (InnerEnum inner){
		Parameter parameter = new Parameter(ENUM_FIELD_TYPE,"NOT_LIKE");
		EnumField enumField = new EnumField("NOT_LIKE","not like 条件") ;
		enumField.addParameter(parameter, "NOT LIKE");
		inner.addEnumFields(enumField);
	}

	private void addField(InnerEnum inner ){
		Field field = new Field("value", ENUM_FIELD_TYPE) ;
		field.setUseDefaultValue(false);
		Decoration d = field.getDecoration();
		d.setFinal(true);
		inner.addField(field)	 ;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractClass#getDecoration()
	 */
	@Override
	protected ClassDecoration getDecoration() throws CompilationException {
		ClassDecoration answer = super.getDecoration();
		answer.setVisibility(JavaVisibility.PUBLIC);
		
		return answer ; 
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractClass#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		
		return JAVA_DOC;
	}

	@Override
	protected FullyQualifiedJavaType getType() {
		return null;
	}
}
